In [2]:
import numpy as np
import theano
import theano.tensor as T

Exercises

1. Logistic function

Create an expression for the logistic function $s(x) = \frac{1}{1+exp(-x)}$. Plot the function and its derivative, and verify that $\frac{ds}{dx} = s(x)(1-s(x))$.


In [14]:
# Uncomment and run this cell for one solution
load ./spoilers/logistic.py


  File "<ipython-input-14-8865f3de47c6>", line 2
    load ./spoilers/logistic.py
          ^
SyntaxError: invalid syntax

2. Fibonacci sequence

Calculate the 3rd to 10th terms of the sequence, defined by the recurrance relation $F_n = F_{n-2} + F_{n-1}$, with $F_1=1$ and $F_2=1$.


In [31]:
# Uncomment and run this cell for one solution
#%load spoilers/fib.py

3. Game of Life

Implement Conway's Game of Life with periodic boundary conditions (wrapping borders).


In [34]:
board = theano.shared(np.zeros((100, 100), dtype='uint8'))

initial = np.random.binomial(1, 0.1, size=(100, 100)).astype('uint8')
board.set_value(initial)

In [ ]:
# Create a function f that updates board with new values and return the current state
# Uncomment the line below and run for a solution
#%load spoilers/life.py

In [44]:
# After creating your f function, run this cell to animate the output
%matplotlib notebook
import matplotlib.pyplot as plt

from IPython import display
import time

for i in range(50):
    plt.gca().cla()
    current = f()
    plt.imshow(current, interpolation='nearest', cmap='gray')
    display.clear_output(wait=True)
    display.display(plt.gcf()) 
    time.sleep(0.1)